package com.bckj.fastboot.core.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class SpringUtils implements ApplicationContextAware {
    
    private static ApplicationContext applicationContext;
    
    @SuppressWarnings("NullableProblems")
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringUtils.applicationContext = applicationContext;
    }

    // 可选：提供获取Bean的便捷方法
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }
    
    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }

    public static <T> Map<String, T> getBeanOfType(Class<T> tClass) {
        return applicationContext.getBeansOfType(tClass);
    }

    public static Environment getEnv() {
        return applicationContext.getEnvironment();
    }

    public static String getProperty(String key) {
        return getEnv().getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
        return getEnv().getProperty(key, defaultValue);
    }

    public static <T> T getProperty(String key, Class<T> clazz) {
        return getEnv().getProperty(key, clazz);
    }

    public static <T> T getProperty(String key, Class<T> clazz, T defaultValue) {
        return getEnv().getProperty(key, clazz, defaultValue);
    }

    public static boolean isProd() {
        return getEnv().matchesProfiles("prod");
    }
}